const {
    DATABASEformatTime
} = require('./utils/util')
// app.js
App({
    globalData: {
        // 后续从服务器获取 敏感数据 请勿修改
        appsecret: '330edeba657d3b03994c01a3134df88a',
        appid: 'wxcaa6d8c2b3206e53',
        // 服务器BaseURL
        baseURL: 'http://www.mmdccj.top:9002',
        // 用户信息
        userInfo: null,
        // 是否为第一次启动
        // isFirstOpen: true,
    },
    onLaunch() {
        // 获取本地存储的openid，否则发送请求获取
        if (wx.getStorageSync('openid') === '') {
            console.log('未获取到本地存储的openid');
            this.getUserInfo()
        } else {
            console.log('获取到本地存储的openid');
            this.getUserInfoFromServer()
            this.getUserLocation()
        }

    },
    // 获取openid
    getUserInfo: function (cb) {
        console.log("获取openid函数");
        let that = this;
        if (this.globalData.userInfo !== null) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            wx.login({
                success: res => {
                    console.log("获取到login 返回的res");
                    console.log(res);
                    const URL = `https://api.weixin.qq.com/sns/jscode2session?appid=${this.globalData.appid}&secret=${this.globalData.appsecret}&js_code=${res.code}&grant_type=authorization_code`
                    wx.request({
                        url: URL,
                        success: function (res) {
                            console.log("成功获取到openid");
                            // res.data中有openid   
                            wx.setStorageSync('openid', res.data.openid)
                            // 从后端获取信息
                            wx.request({
                                url: `${that.globalData.baseURL}/vx/user`,
                                data: {
                                    openid: res.data.openid
                                },
                                success: (res) => {
                                    wx.showToast({
                                        title: `欢迎回来`,
                                        duration: 2000
                                    })
                                }
                            })
                        },
                        fail: res => {
                            console.log('获取openid失败');
                            console.log(res);
                        }
                    })
                },
                fail: res => {
                    console.log("调用微信login API失败");
                    console.log(res);
                }
            })
        }
    },
    // 获取详细信息
    getUserInfoFromServer: function () {
        const openid = wx.getStorageSync('openid');
        wx.request({
            url: `${this.globalData.baseURL}/vx/userinfo`,
            data: {
                openid
            },
            success: (res) => {
                wx.setStorageSync("baseinfo", res.data.data)
            },
            fail: res=>{
                console.log("从服务器端获取用户信息失败");
                console.log(res);
            }
        })
    },
    // 获取行程信息
    getUserLocation: function () {
        const openid = wx.getStorageSync('openid');
        wx.request({
            url: `${this.globalData.baseURL}/vx/userinfo/location`,
            data: {
                openid
            },
            success: (res) => {
                wx.setStorageSync("location", res.data)
                console.log(res);
            }
        })
    }
})